/*
 * Copyright (c) 2014-2020 杭州派迩信息技术有限公司 All Rights Reserved.
 * ProjectName: pi-flowable-project
 * ModuleName: pi-actuator-service
 * FileName: PiActuatorServiceInstanceController.java
 * Author: kai.li
 * Date: 2021/08/31 18:16:31
 * Version: 1.0
 * LastModified: 2021/08/31 18:16:31
 */

package com.pisolution.actuator.service.controller;

import com.alibaba.fastjson.JSONObject;
import com.pisolution.actuator.service.exception.ActuatorServiceException;
import com.pisolution.actuator.service.model.PageActionResult;
import com.pisolution.actuator.service.model.PiActuatorServiceInstance;
import com.pisolution.actuator.service.model.ServiceInstanceReq;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.pisolution.actuator.service.service.PiActuatorServiceInstanceService;

import java.util.HashMap;
import java.util.Map;

/**
 * @className: PiActuatorServiceInstanceController
 * @description: 前端控制器
 * @author: kai.li
 * @date: 2021/08/31 18:16:31
 * @since: 1.0
 */

@RestController
@RequestMapping("/pi-actuator-service/")
public class PiActuatorServiceInstanceController {

    @Autowired
    private PiActuatorServiceInstanceService piActuatorServiceInstanceService;

    /**
     * @title 服务注册
     * @description
     * @author admin
     * @updateTime 2021/9/2 10:01
     * @throws
     */
    @PostMapping(value = "/register")
    public Map<String, String> register(@RequestBody Map<String, Object> registerInfo){
        if(registerInfo == null){
            throw new ActuatorServiceException("服务注册参数为空！");
        }
        String id = piActuatorServiceInstanceService.register(registerInfo);
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("id", id);
        return resultMap;
    }

    /**
     * @title 心跳监控
     * @description
     * @author admin
     * @updateTime 2021/9/2 10:01
     * @throws
     */
    @PostMapping(value = "/heartbeat")
    public Map<String, String> heartbeat(@RequestBody Map<String, Object> heartbeatInfo){
        if(heartbeatInfo == null){
            throw new ActuatorServiceException("心跳监控参数为空！");
        }
        String id = piActuatorServiceInstanceService.heartbeat(heartbeatInfo);
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("id", id);
        return resultMap;
    }

    /**
     * @title 服务注销
     * @description
     * @author admin
     * @updateTime 2021/9/2 10:07
     * @throws
     */
    @DeleteMapping("/register/{id}")
    public Map<String, String> deregister(@PathVariable("id") String id){
        String instanceId = piActuatorServiceInstanceService.deregister(id);
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("id", instanceId);
        return resultMap;
    }

    /**
     * @title 分页查询服务实例列表
     * @description
     * @author admin
     * @updateTime 2021/9/15 10:02
     * @throws
     */
    @PostMapping("/pageList")
    public PageActionResult<PiActuatorServiceInstance> pageList(@RequestBody ServiceInstanceReq serviceInstanceReq){
        return piActuatorServiceInstanceService.pageList(serviceInstanceReq);
    }

    /**
     * @title 获取实例health信息
     * @description
     * @author admin
     * @updateTime 2021/9/15 15:09
     * @throws
     */
    @GetMapping("/health/{instanceId}")
    public JSONObject gethealth(@PathVariable(value = "instanceId") Long instanceId){
        return piActuatorServiceInstanceService.gethealth(instanceId);
    }

    /**
     * @title 获取实例info信息
     * @description
     * @author admin
     * @updateTime 2021/9/15 15:09
     * @throws
     */
    @GetMapping("/info/{instanceId}")
    public JSONObject getInfo(@PathVariable(value = "instanceId") Long instanceId){
        return piActuatorServiceInstanceService.getInfo(instanceId);
    }

    /**
     * @title 获取实例env信息
     * @description
     * @author admin
     * @updateTime 2021/9/15 15:09
     * @throws
     */
    @GetMapping("/env/{instanceId}")
    public JSONObject getEnv(@PathVariable(value = "instanceId") Long instanceId){
        return piActuatorServiceInstanceService.getEnv(instanceId);
    }

    /**
     * @title 获取实例metrics信息
     * @description
     * @author admin
     * @updateTime 2021/9/15 15:09
     * @throws
     */
    @GetMapping("/metrics/{instanceId}")
    public JSONObject getMetrics(@PathVariable(value = "instanceId") Long instanceId){
        return piActuatorServiceInstanceService.getMetrics(instanceId, null);
    }

    /**
     * @title 获取实例metrics详细信息信息
     * @description
     * @author admin
     * @updateTime 2021/9/15 15:09
     * @throws
     */
    @GetMapping("/metrics/{instanceId}/{detail}")
    public JSONObject getMetrics(@PathVariable(value = "instanceId") Long instanceId, @PathVariable(value = "detail") String detail){
        return piActuatorServiceInstanceService.getMetrics(instanceId, detail);
    }
}
